//
//  HotVC.m
//  Cat
//
//  Created by dllo on 15/11/26.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

// http://api.huapipi.com/show/v2/hot/top/1?deviceId=1826162&uid=1033268

// http://user.huapipi.com/userinfo/basic.do?uid=829821 // 个人信息

// http://api.huapipi.com/dynamic/l1/detail/2206107?deviceId=1826162&uid=1033268&w=720&loc=collect&channelId=40&h=1280                 // 二级页面

#import "HotVC.h"
#import "CYHHotSecVC.h"
#import "CYHHotOtherVC.h"

@interface HotVC ()<UITableViewDataSource, UITableViewDelegate, RollViewDelegate>

@property (nonatomic, strong) UITableView *HotTableView;
@property (nonatomic, strong) RollView *rollView;
@property (nonatomic, strong) NSMutableArray *tableViewArray;
@property (nonatomic, strong) NSMutableArray *rollArray;
@property (nonatomic, copy) NSString *verStr;
@property (nonatomic, strong) NSMutableArray *openTypeArr;

@end

@implementation HotVC

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tableViewArray = [NSMutableArray array];
        self.rollArray = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //
    [self createView];
    
    //
    [self createData];
    
    // 下拉刷新
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerData)];
    self.HotTableView.header = header;
    
    // 上拉刷新
    MJRefreshAutoGifFooter *footer = [MJRefreshAutoGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerData)];
    self.HotTableView.footer = footer;

}

#pragma mark - createData
- (void)createData {
        
        [ToolNetWorking ToolNetWorking:@"http://api.huapipi.com/show/v2/hot/top/1?deviceId=1826162&uid=1033268" block:^(id result) {
            self.tableViewArray = [NSMutableArray array];
            self.openTypeArr = [NSMutableArray array];
            
            // 轮播图数据
            NSMutableArray *imageArr = [NSMutableArray array];
            imageArr = [HotModel BaseModel:result[@"bannerList"]];
            for (HotModel *model in imageArr) {
                [self.rollArray addObject:model.picUrl];
                [self.openTypeArr addObject:model];
            }
            
            // tableView
            NSMutableArray *arr = [HotModel BaseModel:result[@"rows"]];
            
            // 拼接网址刷新
            HotModel *tempModel = arr.lastObject;
            self.verStr = tempModel.ver;
            
            // 判断类别
            for (HotModel *HotModel in arr) {
                if ([HotModel.ftype isEqualToString:@"tracing"]) {
                    [self.tableViewArray addObject:HotModel];
                }
            }
            
            [self.rollView.RollCollcetionView reloadData];
            [self.HotTableView reloadData];
        }];
}

#pragma mark - MJRefresh
- (void)headerData {
    
    [ToolNetWorking ToolNetWorking:@"http://api.huapipi.com/show/v2/hot/top/1?deviceId=1826162&uid=1033268" block:^(id result) {
        self.tableViewArray = [NSMutableArray array];
        

        // tableView
        NSMutableArray *arr = [HotModel BaseModel:result[@"rows"]];
        
        // 拼接网址刷新
        HotModel *tempModel = arr.lastObject;
        self.verStr = tempModel.ver;
        
        for (HotModel *HotModel in arr) {
            if ([HotModel.ftype isEqualToString:@"tracing"]) {
                [self.tableViewArray addObject:HotModel];
                
            }
        }
        
        [self.HotTableView reloadData];
        [self.HotTableView.header endRefreshing];
    }];
}

- (void)footerData {
    [ToolNetWorking ToolNetWorking:[NSString stringWithFormat:@"http://api.huapipi.com/show/v2/hot/top/1?deviceId=1826162&uid=1033268&ver=%@", self.verStr] block:^(id result) {
        

        // tableView
        NSMutableArray *arr = [HotModel BaseModel:result[@"rows"]];
        
        // 拼接网址刷新
        HotModel *tempModel = arr.lastObject;
        self.verStr = tempModel.ver;
        
        for (HotModel *HotModel in arr) {
            if ([HotModel.ftype isEqualToString:@"tracing"]) {
                [self.tableViewArray addObject:HotModel];
                
            }
        }
        
        [self.HotTableView reloadData];
        [self.HotTableView.footer endRefreshing];
    }];
}



#pragma mark - createView
- (void)createView {
    //
    self.HotTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, VIEW_HEIGHT) style:UITableViewStylePlain];
    self.HotTableView.delegate = self;
    self.HotTableView.dataSource = self;
    [self.view addSubview:self.HotTableView];
    self.HotTableView.separatorStyle = NO;
    
    [self.HotTableView registerClass:[HotCell class] forCellReuseIdentifier:@"HotResue"];
    
    // 轮播图
    self.rollView = [[RollView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, VIEW_HEIGHT / 3) imageArr:self.rollArray];
    self.rollView.RollDelegate = self;
    self.HotTableView.tableHeaderView = self.rollView;
    
}



#pragma mark - tableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.tableViewArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HotCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HotResue"];
    
    HotModel *model = self.tableViewArray[indexPath.row];
    cell.model = model;
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15], NSFontAttributeName, nil];
    // 根据文本字体大小来计算文本的cell高度
    // 第一个参数: 文字显示的最大尺寸范围(375, 0)(0 代表最大的意思)
    // 第二个参数:
    CGRect rect = [[self.tableViewArray[indexPath.row] content][@"word"] boundingRectWithSize:CGSizeMake(VIEW_WIDTH, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return VIEW_HEIGHT / 2 + 230 + rect.size.height;
}



#pragma mark - tabkeViewAction
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    CYHHotOtherVC *otherVC = [[CYHHotOtherVC alloc] init];
    HotModel *model = self.tableViewArray[indexPath.row];
    // 传值
    otherVC.Secid = model.secid;
    
    
//    CATransition *trans=[CATransition animation];
//    
//    trans.type=@"pageCurl";
//    trans.subtype=@"fromTop";
//    
//    [self.view.layer addAnimation:trans forKey:nil];
    
    [self.navigationController pushViewController:otherVC animated:YES];
}



#pragma mark - LBTAction
- (void)pushViewController:(NSIndexPath *)index {
    CYHHotSecVC *HotSecVC = [[CYHHotSecVC alloc] init];
    if (index.item == 0) {
        HotSecVC.openType = self.openTypeArr.lastObject;
    } else if (index.item == self.openTypeArr.count + 1) {
        HotSecVC.openType = self.openTypeArr.firstObject;
    } else {
        HotSecVC.openType = self.openTypeArr[index.item - 1];
    }
    [self.navigationController pushViewController:HotSecVC animated:YES];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self network];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
